//
//  AccountDeatailViewController.m
//  Hxcsfw
//
//  Created by 企友软mac on 2018/5/18.
//  Copyright © 2018年 企友软mac. All rights reserved.
//

#import "AccountDeatailViewController.h"
#import "CoursePurchaseCell.h"
#import "MoneyPurchaseCell.h"
#import "AccountInfoModel.h"

@interface AccountDeatailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arrAccInfo;

@end

@implementation AccountDeatailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    _arrAccInfo = [NSMutableArray array];
    [self requestListDataStrNum:@"1" isRefrsh:YES];
    [self notifyRequestListData];
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WSCREENH_WIDTH, WSCREENH_HEIGHT) style:UITableViewStylePlain];
        self.tableView.estimatedRowHeight =0;
        self.tableView.estimatedSectionHeaderHeight =0;
        self.tableView.estimatedSectionFooterHeight =0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//去掉横线
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrAccInfo.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (self.arrAccInfo.count > 0 && self.arrAccInfo != nil) {
        if ([[self.arrAccInfo[indexPath.row] JFFS] isEqualToString:@"iOS内购支付"]) {
            return 85;
        } else {
            return 128;
        }
    }
    return 85;

}

//设置UITableViewCell的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];

    if (self.arrAccInfo.count > 0 && self.arrAccInfo != nil) {
        if ([[self.arrAccInfo[indexPath.row] JFFS] isEqualToString:@"iOS内购支付"]) {

            cell = [tableView dequeueReusableCellWithIdentifier:@"iden"];
            if (!cell) {
                MoneyPurchaseCell *moneyPurchaseCell = [[[NSBundle mainBundle] loadNibNamed:@"MoneyPurchaseCell" owner:self options:nil] lastObject];
                moneyPurchaseCell.lbMoneyLeft.text = [NSString stringWithFormat:@"%@元",[self.arrAccInfo[indexPath.row] KC_PRICE]];
                moneyPurchaseCell.lbMoneyRight.text = [NSString stringWithFormat:@"+%.2f",[[self.arrAccInfo[indexPath.row] KC_PRICE] floatValue]];
                cell = moneyPurchaseCell;
            }

        } else {
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            if (!cell) {
                CoursePurchaseCell *coursePurchaseCell = [[[NSBundle mainBundle] loadNibNamed:@"CoursePurchaseCell" owner:self options:nil] lastObject];
                coursePurchaseCell.lbDate.text = [WShareUtil timeFormat:[self.arrAccInfo[indexPath.row] JFRQ]];
                coursePurchaseCell.lbContent.text = [self.arrAccInfo[indexPath.row] KC_NAME];
                coursePurchaseCell.lbMoney.text = [NSString stringWithFormat:@"-%.2f",[[self.arrAccInfo[indexPath.row] KC_PRICE] floatValue]];
                cell = coursePurchaseCell;
            }
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//行点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}

- (void)notifyRequestListData{
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [WShareUtil sharedData].j = 1;
        [self requestListDataStrNum:@"1" isRefrsh:YES];
    }];

    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

        NSString *num = [NSString stringWithFormat:@"%d",++[WShareUtil sharedData].j];
        [self requestListDataStrNum:num isRefrsh:NO];
    }];
}

#pragma mark - 下载列表数据
//- (void)requestListDataStrNum:(NSString *)pageNum isRefresh:(BOOL)isRefresh{
//
//    [WShareUtil sharedData].isShowWait = NO;
//
//    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"xxfb",@"content":@{@"revert":@"paginate",@"colcode":@"57",@"pagesize":@"10",@"pagenumber":pageNum}};
//    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
//
//    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
//        WLog(@"------%@",responseObject);
//        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
//        NSString *messageStr = [JsonDic objectForKey:@"message"];
//        NSString *successStr = [JsonDic objectForKey:@"success"];
//        // 解密请求回的数据（字典）
//        NSString *returnStr = [CommonFunc decryptWithText:messageStr];

//        // json字符串转字典
//        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];
//
//        WLog(@"%@",returnDic[@"data"]);
//        if (isRefresh) {
//            [[WShareUtil sharedData].arrMdiagram removeAllObjects];
//        }
//        for (NSDictionary *dic in returnDic[@"data"]) {
////            DiagramModel *diagramModel = [DiagramModel mj_objectWithKeyValues:dic];
////            [[WShareUtil sharedData].arrMdiagram addObject:diagramModel];
//        }
//
//        [self.tableView reloadData];
//        [self noWifiRefershingEnd];
//        ///* --- 图片式提醒
//        UIView *view = [self.tableView viewWithTag:10000];
//        [view removeFromSuperview];
//        HHNonePage *_nonePage = [[HHNonePage alloc]init];
//        _nonePage = nil;
//        if ([WShareUtil sharedData].arrMdiagram.count == 0) {
//            _nonePage = [[HHNonePage alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//            _nonePage.tag = 10000;
//            [self.view addSubview:_nonePage];
//        }
//        if ([returnDic[@"data"] count] < 10) {
//            // 拿到当前的上拉刷新控件，变为没有更多数据的状态
//            [self.tableView.footer endRefreshingWithNoMoreData];
//        }
//
//    } failure:^(NSError *error) {
//        WLog(@"失败");
//        [self noWifiRefershingEnd];
//    } showProgress:NO];
//}

- (void)noWifiRefershingEnd {
    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];
    [WShareUtil sharedData].isShowWait = YES;
}

- (void)requestListDataStrNum:(NSString *)pageNum isRefrsh:(BOOL)isRefrsh{

    [WShareUtil sharedData].isShowWait = NO;

    NSString *nologinuser = [WShareUtil getUUID];
    NSDictionary *parametersDict;
    if ([WShareUtil sharedData].dicSession) {
        parametersDict = @{@"content": @{@"dto": @{@"page":pageNum,@"limit": @"10",@"mode":@"recharge"},@"revert": @"list",@"username":[WShareUtil sharedData].dicSession[@"nsrsbh"],@"apptype":@"ios"},@"mode": @"native",@"service":@"order"};
    } else {
        parametersDict = @{@"content": @{@"dto": @{@"page":pageNum,@"limit": @"10",@"mode":@"recharge"},@"revert": @"list",@"nologinuser":nologinuser,@"apptype":@"ios"},@"mode": @"native",@"service":@"order"};
    }
    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};


    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];

        if ([successStr isEqualToString:@"false"]) {
            return;
        }
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];
        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];

        if (isRefrsh) {
            [self.arrAccInfo removeAllObjects];
        }

        NSArray *arrData = returnDic[@"data"];

        for (NSDictionary *dic in arrData) {
            AccountInfoModel *accountInfoModel = [AccountInfoModel mj_objectWithKeyValues:dic];
            [self.arrAccInfo addObject:accountInfoModel];
        }
        [self.tableView reloadData];
        [self noWifiRefershingEnd];

        ///* --- 图片式提醒
        UIView *view = [self.tableView viewWithTag:10000];
        [view removeFromSuperview];
        HHNonePage *_nonePage = [[HHNonePage alloc]init];
        _nonePage = nil;
        if (arrData.count == 0) {
            _nonePage = [[HHNonePage alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            _nonePage.tag = 10000;
            [self.view addSubview:_nonePage];
        }
        if ([returnDic[@"data"] count] < 10) {
            // 拿到当前的上拉刷新控件，变为没有更多数据的状态
            [self.tableView.footer endRefreshingWithNoMoreData];
        }

    } failure:^(NSError *error) {
        [self noWifiRefershingEnd];
        WLog(@"失败");
    } showProgress:YES];
}


@end
